using System;
using Gtk;
using MonoBED3;
using br.odb.portalizer;
//-----------------------------------------------------------------------------
public partial class MainWindow : Gtk.Window, ContextListener, br.odb.utils.FileServerDelegate
{
	public static EditorContext editContext;
	public static int altButtonBehaviour;
	private static MainWindow instance;
	
	public static MainWindow getInstance() {
		return instance;
	}
	
//-----------------------------------------------------------------------------	
	public MainWindow () : base(Gtk.WindowType.Toplevel)
	{
		Build ();
		editContext = null;
		instance = this;
		createNewContext();
		
		br.odb.libscene.World.snapLevel = 3;
	}
//-----------------------------------------------------------------------------
	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		if ( onQuitApp() ) {
			
			Application.Quit ();
			a.RetVal = true;
		}
	}
//-----------------------------------------------------------------------------	
	protected virtual void OnExposeEvent (object o, Gtk.ExposeEventArgs args)
	{
		this.tblViewsLayout.ShowAll();
	}
//-----------------------------------------------------------------------------	
	protected virtual void OnNewActionActivated (object sender, System.EventArgs e)
	{
		if (editContext != null && editContext.isCommitNeeded())
		{
			editContext.commit();
		}
		
		createNewContext();
	}	
//-----------------------------------------------------------------------------
	private void broadcastContext( EditorContext context ) {		
		
		this.view1.setContext(context);
		this.view2.setContext(context);
		this.view3.setContext(context);
		this.view4.setContext(context);				
	}
	
	private void createNewContext()
	{

		editContext = new EditorContext(this);	
		editContext.setBroadcaster( this );
		broadcastContext( editContext );
		updateWindowTitle();
	}	
	
	protected virtual void OnSaveAsActionActivated (object sender, System.EventArgs e) {
		onSaveMapWithDialogChooser();		
	}
	
	private void updateWindowTitle() {
		
		String newTitle = "MonoBED - ";
		
		if ( editContext.isCommitNeeded() ) {
			newTitle += "[modified]";
		}
		
		if ( editContext.getFilename() != null )
			newTitle += editContext.getFilename();
		else
			newTitle += "new file";
		
		Title = newTitle;		
	}
	
	public void contextModified() {
		broadcastContextModified();
		updateWindowTitle();
	}
	
	public void broadcastContextModified() {
		Logger.log("broadcasting modified");
		( ( ContextListener )view1 ).contextModified();
		( ( ContextListener )view2 ).contextModified();
		( ( ContextListener )view3 ).contextModified();
		( ( ContextListener )view4 ).contextModified();
	}
	
	protected virtual void OnGuessLinksClicked (object sender, System.EventArgs e) {
		//re-implement link guessing...
	}
	
	protected virtual void OnPropertiesAction1Activated (object sender, System.EventArgs e) {
		
		Logger.log( "importando de OBJ" );

		FileChooserDialog chooser = new FileChooserDialog( "Please select a mesh to import ...",
													        this,
													        FileChooserAction.Open,
													        "Cancel", ResponseType.Cancel,
													        "Open", ResponseType.Accept );
												        
        if( chooser.Run() == ( int ) ResponseType.Accept ) {

			br.odb.libscene.World world = null;
			chooser.Hide();
			//finally start processing
			java.util.ArrayList mesh = null;				
			br.odb.liboldfart.parser.FileFormatParser loader = null;		
			world = new br.odb.libscene.World();
			loader = new br.odb.liboldfart.wavefront_obj.WavefrontOBJLoader();
			loader.preBuffer( this.openAsInputStream( chooser.Filename ) );
			loader.parseDocument( this );		
			mesh = loader.getGeometry();	
			br.odb.portalizer.WorldPartitioner.buildConvexHulls( world, mesh );
			createNewContext();
			editContext.setBroadcaster( this );
			editContext.internalize( world, false, true );
			Title = "MonoBED - " + chooser.Filename.ToString();
			editContext.askForScreenRefresh();				
     	}	
		
		chooser.Destroy();	
	}
	
	
	
	protected virtual void OnConvertAction4Activated (object sender, System.EventArgs e) {
		
		Logger.log( "exportando level" );
		
		
		
		Gtk.FileChooserDialog fc = new Gtk.FileChooserDialog( "Please name the file to save",
		                                                      this,
		                                                      FileChooserAction.Save,
		                                                      "Cancel",
		                                                      ResponseType.Cancel,
		                                                      "Save",
		                                                      ResponseType.Accept );	 	

        if ( fc.Run() == ( int )ResponseType.Accept ) {						
			String filename =  fc.Filename.ToString();
			fc.Destroy();
			
			java.util.ArrayList people = new java.util.ArrayList();
			br.odb.libscene.World worldBefore =  editContext.getWorld().makeBZK3World();
			br.odb.libscene.World world = WorldPartitioner.optimize( worldBefore );

			WorldPartitioner.guessLinks( world, true );				
//			world.addMasters( world );		
//			
//			WorldPartitioner.transientStartSector = 1;
//			br.odb.libscene.Actor actor = new br.odb.libscene.Actor();
//			actor.setCurrentSector( WorldPartitioner.transientStartSector );
//			people.add( actor );				
			world.saveToDiskAsLevel( this.openAsOutputStream( filename ), people, false );
			
			
			
            MessageDialog md = new MessageDialog (null, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, "Export suceeded.");
            md.Run ();
            md.Destroy();			
		}				
	}
	
	
	
	
	
	protected virtual void OnSaveActionActivated (object sender, System.EventArgs e) {
		onSave();
	}
	
	private void onSave() {
		
		if ( editContext.getFilename() == null ||  editContext.getFilename() == "" )
			onSaveMapWithDialogChooser();
		else
			editContext.saveCurrent();
		
		updateWindowTitle();
	}
	
	private void onSaveMapWithDialogChooser() {
		Gtk.FileChooserDialog fc = new Gtk.FileChooserDialog( "Please name the file to save",
		                                                      this,
		                                                      FileChooserAction.Save,
		                                                      "Cancel",
		                                                      ResponseType.Cancel,
		                                                      "Save",
		                                                      ResponseType.Accept );	 	

        if ( fc.Run() == ( int )ResponseType.Accept ) {
	       	editContext.save( fc.Filename.ToString() );	
			updateWindowTitle();
			fc.Destroy();
		}		
	}
	
	protected virtual void OnOpenAction1Activated (object sender, System.EventArgs e)
	{
		onOpenMapWithDialogChooser();
	}
	
	protected virtual void OnOpenActionActivated (object sender, System.EventArgs e) {
		onOpenMapWithDialogChooser();
	}
	
	
	
	private void onOpenMapWithDialogChooser() {
		onOpenMapWithDialogChooser( true );
	}
		
	private void onOpenMapWithDialogChooser( bool openMasters ) {
	
		FileChooserDialog chooser = new FileChooserDialog( "Please select a map to open ...",
													        this,
													        FileChooserAction.Open,
													        "Cancel", ResponseType.Cancel,
													        "Open", ResponseType.Accept );
												        
        if( chooser.Run() == ( int ) ResponseType.Accept ) {

			createNewContext();
			editContext.setBroadcaster( this );
			chooser.Hide();
			editContext.loadSnapshot( chooser.Filename, openMasters );
			Title = "MonoBED - " + chooser.Filename.ToString();
     	}	
		
		chooser.Destroy();
	}
	
	protected virtual void OnConvertAction5Activated (object sender, System.EventArgs e)
	{
		FileChooserDialog chooser = new FileChooserDialog( "Please select a map to open ...",
													        this,
													        FileChooserAction.Open,
													        "Cancel", ResponseType.Cancel,
													        "Open", ResponseType.Accept );
												        
        if( chooser.Run() == ( int ) ResponseType.Accept ) {

			createNewContext();
			editContext.setBroadcaster( this );
			chooser.Hide();
			editContext.importBZK2( chooser.Filename, true );
			Title = "MonoBED - " + chooser.Filename.ToString();
     	}	
		
		chooser.Destroy();		
	}
	
	
	protected virtual void OnImportCompiledMapActivated (object sender, System.EventArgs e)
	{
		onOpenMapWithDialogChooser( false );
	}
	
	
	
	
	
	
	
	
	
//-----------------------------------------------------------------------------	


	
	
	public java.io.InputStream openAsInputStream( string filename) {
		
		return new java.io.FileInputStream( filename );
	}

	
	public java.io.OutputStream openAsOutputStream( string filename) {
		return new java.io.FileOutputStream( filename );
	}	

	protected void OnQuitActionActivated (object sender, System.EventArgs e) {
		onQuitApp();
	}
	

	
	private bool onQuitApp() {
		
		if ( editContext != null && editContext.isCommitNeeded() ) {

			MessageDialog md = new MessageDialog ( this, 
                                      DialogFlags.DestroyWithParent,
	                              MessageType.Question, 
                                      ButtonsType.YesNo, "You have unsaved changed. Do you want to save?");
				
			ResponseType result = (ResponseType)md.Run ();
			
			if (result == ResponseType.Yes) {
				this.onSave();	
			}

			md.Destroy();
		}
		this.Destroy();
		return true;
	}

	protected void OnDestroyEvent (object o, Gtk.DestroyEventArgs args)
	{
		onQuitApp();
	}

	protected void OnConvertAction10Activated (object sender, System.EventArgs e)
	{
		throw new System.NotImplementedException ();
	}


}
//-----------------------------------------------------------------------------